package com.hs.room.repository

import android.content.Context
import com.hs.room.AppDatabase
import com.hs.room.model.archive.AlarmLog
import com.hs.room.model.archive.ArchiveApproval
import com.hs.room.model.archive.ArchiveChange
import com.hs.room.model.archive.ArchiveLog
import com.hs.room.model.archive.OptLog
import com.hs.room.repository.service.ILogRepository
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.emitAll
import kotlinx.coroutines.flow.flow
import javax.inject.Inject

/**
 * 日志记录相关仓库
 */
class LogRepository @Inject constructor(
    @ApplicationContext var context: Context,
    val db: AppDatabase
) : ILogRepository {

    override suspend fun insert(d: ArchiveApproval) {
        db.archiveApprovalDao().insert(d)
    }

    override suspend fun insert(d: AlarmLog) {
        db.alarmLogDao().insert(d)
    }

    override suspend fun insert(d: ArchiveChange) {
        db.archiveChangeDao().insert(d)
    }

    override suspend fun insert(d: ArchiveLog) {
        db.archiveLogDao().insert(d)
    }

    override suspend fun insert(d: OptLog) {
        db.optLogDao().insert(d)
    }

    override fun getArchiveLog(limit: Int, offset: Int): Flow<List<ArchiveLog>> {
        return flow {
            emitAll(db.archiveLogDao().get(offset, offset * limit))
        }.catch { e ->
            // 进行异常处理
            println("Error occurred while fetching getType data: $e")
        }
    }

    override fun getArchiveApproval(limit: Int, offset: Int): Flow<List<ArchiveApproval>> {
        return flow {
            emitAll(db.archiveApprovalDao().get(offset, offset * limit))
        }.catch { e ->
            // 进行异常处理
            println("Error occurred while fetching getType data: $e")
        }
    }

    override fun getOptLog(limit: Int, offset: Int): Flow<List<OptLog>> {
        return flow {
            emitAll(db.optLogDao().get(offset, offset * limit))
        }.catch { e ->
            // 进行异常处理
            println("Error occurred while fetching getType data: $e")
        }
    }

    override fun getLastOpt(): Flow<OptLog> {
        return flow {
            emit(db.optLogDao().getLast())
        }.catch { e ->
            // 进行异常处理
            println("Error occurred while fetching getType data: $e")
        }
    }

    override fun getArchiveChange(limit: Int, offset: Int): Flow<List<ArchiveChange>> {
        return flow {
            emitAll(db.archiveChangeDao().get(offset, offset * limit))
        }.catch { e ->
            // 进行异常处理
            println("Error occurred while fetching getType data: $e")
        }
    }

    override fun getAlarmLog(limit: Int, offset: Int): Flow<List<AlarmLog>> {
        return flow {
            emitAll(db.alarmLogDao().get(offset, offset * limit))
        }.catch { e ->
            // 进行异常处理
            println("Error occurred while fetching getType data: $e")
        }
    }

    override fun countAlarmLog(): Int {
        return db.alarmLogDao().count()
    }

    override fun countArchiveApproval(): Int {
        return db.archiveApprovalDao().count()
    }

    override fun countArchiveChange(): Int {
        return db.archiveChangeDao().count()
    }

    override fun countOptLog(): Int {
        return db.optLogDao().count()
    }

    override fun countArchiveLog(): Int {
        return db.archiveLogDao().count()
    }
}